﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using FixtureManageSystem.ViewModels;


namespace FixtureManageSystem.Views
{
    /// <summary>
    /// BorrowReturn.xaml 的交互逻辑
    /// </summary>
    public partial class BorrowReturn : Page
    {
        public BorrowReturn()
        {

            DataContext = new BorrowReturnViewMode();
            InitializeComponent();
            startTime.SelectedDate = null;
            startTime.DisplayDate = DateTime.Now;
            endTime.SelectedDate = null;
            endTime.DisplayDate = DateTime.Now;
        }

        private void dataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
        {
            BorrowReturnViewMode whiViewMode = this.DataContext as BorrowReturnViewMode;
            whiViewMode.Update();
        }

        private void PersonCombobox_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            BorrowReturnViewMode whiViewMode = this.DataContext as BorrowReturnViewMode;
            whiViewMode.SearchText = this.PersonCombobox.Text;
            string tempText = whiViewMode.SearchText;
            PersonCombobox.IsDropDownOpen = true;
            PersonCombobox.SelectedItem = null;
            PersonCombobox.Text = tempText;
            // 将光标移动到文本框末尾
            MoveCaretToEnd(PersonCombobox);
        }

        private void PersonCombobox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            MoveCaretToEnd(PersonCombobox);
        }

        private void MoveCaretToEnd(ComboBox comboBox)
        {
            comboBox.Focus();
            TextBox textBox = comboBox.Template.FindName("PART_EditableTextBox", comboBox) as TextBox;

            if (textBox != null)
            {
                textBox.CaretIndex = textBox.Text.Length;
            }
        }
    }
}
